/******************************************************
 *                                                     
 *              Score System               
 *                                                     
 *******************************************************/

/**
 * Utility.java
 *
 * 工具类
 *
 * @author sundy
 * @version 0.0.1
 * lastModified 2011-01-01
 */

package com.sundy.core;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sundy.daosupport.SubjectTblDAO;
import com.sundy.service.StudentTbl;
import com.sundy.service.Subject2desc;
import com.sundy.service.SubjectTbl;
import com.sundy.tools.WebUtility;

public class Utility {

	private static final String contextTag = "ctx";

	public static Context obtainContext(HttpServletRequest request) {
		HttpSession session = request.getSession(true);
		Context ctx = (Context) session.getAttribute(contextTag);
		if (ctx == null) {
			ctx = new Context();
			session.setAttribute(contextTag, ctx);
		}
		return ctx;
	}

	public static void showJSP(HttpServletRequest request,
			HttpServletResponse response, String page) throws ServletException,
			IOException {
		RequestDispatcher dispatcher = request.getRequestDispatcher(page);
		dispatcher.forward(request, response);
	}

	public static void showInfor(HttpServletRequest request,
			HttpServletResponse response, String infor)
			throws ServletException, IOException {
		RequestDispatcher dispatcher = request
				.getRequestDispatcher("/information.jsp");
		request.setAttribute("infor", infor);
		dispatcher.forward(request, response);
	}

	public static void showError(HttpServletRequest request,
			HttpServletResponse response, String error)
			throws ServletException, IOException {
		RequestDispatcher dispatcher = request
				.getRequestDispatcher("/error.jsp");
		request.setAttribute("infor", error);
		dispatcher.forward(request, response);
	}

	public static String getDate() {
		return getDate("yyyy年MM月dd日");
	}

	public static String getDate(String pattern) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(pattern);
			return sdf.format(new Date());
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

	public static String getFileName(String path) {
		String splitChar = "\\";
		int pos = path.lastIndexOf(splitChar);
		if (pos == -1)
			return path;
		return path.substring(pos + 1);
	}

	public static String getSubjectById(int id) {
		SubjectTblDAO dao = SubjectTblDAO
				.getFromApplicationContext(Common.applicationContext);
		SubjectTbl item = dao.findById(id);
		if (item == null)
			return "";
		return WebUtility.getRawSubjectCaption(item.getCaption());
	}

	public static int getScore(StudentTbl student, Subject2desc subject) {
		SubjectTblDAO dao = SubjectTblDAO
				.getFromApplicationContext(Common.applicationContext);
		SubjectTbl sub = dao.findById(subject.getSubjectId());
		String scoreStr = WebUtility.getStudentScore(student, sub);
		if (scoreStr == null || scoreStr.equals(""))
			return 0;
		return Integer.valueOf(scoreStr);
	}
}
